/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [MainFrame.java]
 * Creation Date        [22-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_03_game.tankwar_local.version_3;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * 坦克大战主窗口
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class MainFrame extends Frame {

	private static final long serialVersionUID = -3154650792693410416L;

	/** 游戏窗口大小 */
	public static final int GAME_WIDTH = Toolkit.getDefaultToolkit()
			.getScreenSize().width / 2;
	public static final int GAME_HEIGHT = Toolkit.getDefaultToolkit()
			.getScreenSize().height / 2;

	/** 缓冲图片 */
	private Image image = null;

	/** 我的坦克 */
	Tank tank = new Tank(GAME_WIDTH / 2, GAME_HEIGHT / 2, true, this);
	/** 敌人坦克 */
	Set<Tank> enemyTanks = new HashSet<Tank>();

	/** 导弹 */
	List<Missile> missiles = new ArrayList<Missile>();

	/** 初始化游戏界面 */
	private void init() {
		// 初始化5辆敌人坦克
		while (true) {
			Tank enemyTank = new Tank(new Random().nextInt(GAME_WIDTH),
					new Random().nextInt(GAME_HEIGHT) + 30, false, this);
			enemyTanks.add(enemyTank);
			if (enemyTanks.size() == 5) {
				break;
			}
		}

		// 设置主窗口标题
		setTitle("TANK_WAR");
		// 设置主窗口大小
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		// 设置主窗口背景色
		setBackground(Color.DARK_GRAY);
		// 设置主窗口不可变化
		setResizable(false);
		// 设置主窗口可视
		setVisible(true);
		// 设置关闭操作
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// 添加键盘事件监听
		addKeyListener(new MyKeyListener());

		new Thread(new PaintThread()).start();
	}

	/** 绘制坦克 */
	@Override
	public void paint(Graphics g) {
		for (int i = 0; i < missiles.size(); i++) {
			Missile missile = missiles.get(i);
			missile.hitTanks(enemyTanks);
			missile.paint(g);
		}
		tank.paint(g);

		try {
			for (Tank enemyTank : enemyTanks) {
				enemyTank.paint(g);
			}
		} catch (ConcurrentModificationException e) {
		}

		// 绘制游戏信息
		Color color = g.getColor();
		g.setColor(Color.WHITE);
		g.drawString("剩余导弹数: " + missiles.size(), 900, 50);
		g.setColor(color);
	}

	/**
	 * 使用双缓冲技术消除屏幕闪烁 将所有内容画在虚拟图片上, 一次性显示出来, 与机器的性能有关
	 */
	@Override
	public void update(Graphics g) {
		if (image == null) {
			image = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		// 图片画笔
		Graphics gImage = image.getGraphics();
		// 重新绘制背景色, 防止出现拖尾现象
		Color color = gImage.getColor();
		gImage.setColor(Color.WHITE);
		gImage.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gImage.setColor(color);
		// 将图像画到图片上
		paint(gImage);
		// 将画布(image)画到屏幕上
		g.drawImage(image, 0, 0, null);
	}

	public MainFrame() {
		init();
	}

	private class PaintThread implements Runnable {

		@Override
		public void run() {
			while (true) {
				MainFrame.this.repaint();
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 键盘监听器
	 * 
	 * @author 不落的太阳(Sean Yang)
	 */
	private class MyKeyListener extends KeyAdapter {
		@Override
		public void keyPressed(KeyEvent e) {
			tank.keyPressed(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			tank.keyReleased(e);
		}
	}
}
